package com.example.administrator.exkotlin.coroutine

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.example.administrator.exkotlin.R
import com.example.administrator.exkotlin.coroutine.net.RetrofitCreator
import com.example.administrator.exkotlin.coroutine.viewModel.DataViewModel
import com.example.administrator.exkotlin.databinding.ActivityCoroutineBinding
import kotlinx.android.synthetic.main.activity_coroutine.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class CoroutineActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_coroutine)
        //        initData()
        val databing = DataBindingUtil.setContentView<ActivityCoroutineBinding>(this, R.layout.activity_coroutine)

        databing.lifecycleOwner = this
        val vm = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(DataViewModel::class.java)
        //双向绑定
        databing.testData = vm
        //刷新数据
        vm.refreshData()

    }


    private fun initData() {
        CoroutineScope(Dispatchers.Main).launch {
            //挂起
            val data = RetrofitCreator.speedService.getSpeedInfo()
            tv_show.text = data.toString()
        }
    }
}
